package com.lagou.edu.service.impl;

import com.lagou.edu.dao.LagouTokenDao;
import com.lagou.edu.dao.LagouUserDao;
import com.lagou.edu.entity.LagouToken;
import com.lagou.edu.entity.LagouUser;
import com.lagou.edu.service.CodeDubboService;
import com.lagou.edu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;
import java.util.UUID;

/**
 * @author zhangjinshan
 * @version 1.0
 * @description
 * @date 2021/8/25 下午8:37
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private LagouUserDao lagouUserDao;

    @Autowired
    private LagouTokenDao lagouTokenDao;

    @Reference(check = false)
    private CodeDubboService codeDubboService;

    @Autowired(required = false)
    private HttpServletResponse response;


    @Override
    public Integer register(String email, String password, String code) {
        Integer validate = codeDubboService.validate(email, code);
        if (validate != 0) {
            return validate;
        }
        LagouUser lagouUser = LagouUser.builder()
                .email(email)
                .password(password)
                .build();
        lagouUserDao.save(lagouUser);
        return validate;
    }



    @Override
    public String login(String email, String password) {
        LagouUser condition = LagouUser.builder()
                .email(email)
                .password(password)
                .build();
        Example<LagouUser> example = Example.of(condition);
        Optional<LagouUser> optional = lagouUserDao.findOne(example);
        if (ObjectUtils.isEmpty(optional)) {
            return null;
        }
        LagouUser lagouUser = optional.get();
        if (ObjectUtils.isEmpty(lagouUser)) {
            return null;
        }
        String uuid = UUID.randomUUID().toString();
        LagouToken lagouToken = LagouToken.builder()
                .email(email)
                .token(uuid)
                .build();
        lagouTokenDao.save(lagouToken);
        //重定向
        RedirectAttributes redirectAttributes = new RedirectAttributesModelMap();
        redirectAttributes.addFlashAttribute("email", email);
        redirectAttributes.addFlashAttribute("token", uuid);
        //添加cookie
        Cookie cookie = new Cookie("token", uuid);
        cookie.setMaxAge(7*24*60*60);
        cookie.setPath("/");
        response.addCookie(cookie);
        return lagouUser.getEmail();
    }

    @Override
    public String info(String token) {
        LagouToken condition = new LagouToken();
        condition.setToken(token);
        Example<LagouToken> example = Example.of(condition);
        Optional<LagouToken> optional = lagouTokenDao.findOne(example);
        if (ObjectUtils.isEmpty(optional)) {
            return null;
        }
        return optional.get().getEmail();
    }
}
